package yt.kpool.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.king.base.adapter.ViewHolderAdapter;
import com.king.base.adapter.holder.ViewHolder;

import java.util.List;

import yt.kpool.MainActivity;
import yt.kpool.R;
import yt.kpool.bean.ChildAccountBean;
import yt.kpool.util.Util;


/**
 * 挖矿账户列表选择适配器
 * Created by Administrator on 2017/7/17.
 */

public class PersonSelectListviewAdapter extends ViewHolderAdapter<ChildAccountBean.DataBean> {

    private int selectedPos;//选中的id 没有为-1

    private List<ChildAccountBean.DataBean> listData;

    private boolean IsTotal;//是否带全部合计

    private String blanceunit;//余额单位
    public PersonSelectListviewAdapter(Context context, List<ChildAccountBean.DataBean> listData) {
        super(context, listData);
        this.listData=listData;
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, ChildAccountBean.DataBean dataBean, int position) {
        return layoutInflater.inflate(R.layout.item_person_select_layout,null);
    }

    @Override
    public void bindViewDatas(ViewHolder holder, ChildAccountBean.DataBean dataBean, int position) {
               holder.setText(R.id.tv_name,dataBean.getChildAccountName());

               holder.setText(R.id.tv_num, Util.FormatHashrate(dataBean.getHashrate())+"");
               holder.setText(R.id.tv_blance, Util.GetNumber(dataBean.getBalance() + ""));
               holder.setText(R.id.tv_blanceunit, getBlanceunit());
        if (dataBean.getChildAccountId()==selectedPos)
        {
            //选中的项
            ((TextView)holder.getView(R.id.tv_name)).setTextColor(context.getResources().getColor(R.color.white));
            ((TextView)holder.getView(R.id.tv_num)).setTextColor(context.getResources().getColor(R.color.white));
            ((TextView)holder.getView(R.id.tv_blance)).setTextColor(context.getResources().getColor(R.color.white));
            ((TextView)holder.getView(R.id.tv_blanceunit)).setTextColor(context.getResources().getColor(R.color.white));

            holder.getConvertView().setBackgroundColor(context.getResources().getColor(R.color.system_color));

        }else
        {
            ((TextView)holder.getView(R.id.tv_name)).setTextColor(context.getResources().getColor(R.color.black2));
            ((TextView)holder.getView(R.id.tv_num)).setTextColor(context.getResources().getColor(R.color.black3));
            ((TextView)holder.getView(R.id.tv_blance)).setTextColor(context.getResources().getColor(R.color.black3));
            ((TextView)holder.getView(R.id.tv_blanceunit)).setTextColor(context.getResources().getColor(R.color.black3));

            holder.getConvertView().setBackgroundColor(context.getResources().getColor(R.color.white));
        }


    }

    public String getBlanceunit() {
        return blanceunit;
    }

    public void setBlanceunit(String blanceunit) {
        this.blanceunit = blanceunit;
    }

    public boolean isTotal() {
        return IsTotal;
    }

    /**
     * 设置列表是否有合计
     * @param total
     */
    public void setTotal(boolean total) {
        IsTotal = total;
        if (isTotal())
        {
            if (listData.size()>0&&listData.get(0).getChildAccountId()==MainActivity.TOTALID)
            {
                return;
            }
            ChildAccountBean.DataBean dataBean = new ChildAccountBean.DataBean();
            dataBean.setChildAccountName("合计");
            double f=0.0;
            for (int i = 0; i < listData.size(); i++) {
                f=f+listData.get(i).getHashrate();
            }
            dataBean.setHashrate(f);
            dataBean.setChildAccountId(MainActivity.TOTALID);
            listData.add(0,dataBean);
            notifyDataSetChanged();
        }else
        {

            if (listData.size()>0)
            {
                if ( listData.get(0).getChildAccountId()==MainActivity.TOTALID)
                {
                    listData.remove(0);
                    notifyDataSetChanged();
                }

            }
        }
    }

    /**
     * 设置选中
     * @param i
     */
    public void setSelected(int i)
    {
        selectedPos=i;
        notifyDataSetChanged();
    }


    /**
     *获取选中的名字
     */
    public String getSelectedName() {
        return listData.get(selectedPos).getChildAccountName();
    }
}
